﻿@model LogisticsPackageModel
@using Nop.Admin.Models.Logistics;
@using Nop.Core.Domain.Shipping;
@using Nop.Core.Domain.Logistics;
@using Telerik.Web.Mvc.UI;

@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.Telerik().TabStrip().Name("package-edit").Items(x =>
{
    var tabPkgInfo = x.Add().Text(T("Admin.Logistics.Package.Info").Text).Content(TabPkgInfo().ToHtmlString());

    var tabPlanNote = x.Add().Text(T("Admin.Logistics.Common.Fields.PlanNote").Text).Content(TabPlanNote().ToHtmlString());

    var tabPackageNote = x.Add().Text(T("Admin.Logistics.Common.Fields.PackageNote").Text).Content(TabPackageNote().ToHtmlString());

    var selectedTab = (string)ViewData["selectedTab"];
    switch (selectedTab)
    {
        case "info":
            tabPkgInfo.Selected(true);
            break;
        case "plannote":
            tabPlanNote.Selected(true);
            break;
        case "packagenote":
            tabPackageNote.Selected(true);
            break;
        default:
            tabPkgInfo.Selected(true);
            break;
    }

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "package-edit"));
})

@helper TabPkgInfo()
{
    <table class="adminContent">

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.OrderId):
            </td>
            <td class="adminData">
                <a href="@Url.Action("Edit", "Order", new { id = Model.OrderId })">@Model.OrderId</a>
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.ShipmentId):
            </td>
            <td class="adminData">
                <a href="@Url.Action("ShipmentDetails", "Order", new { id = Model.ShipmentId })">@Model.ShipmentId</a>
            </td>
        </tr>
    </table>

    <table class="adminContent" style="padding: 5px;">
        @if (Model.UpdatePackage)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.Status):
                </td>
                <td class="adminData">
                    @Html.DropDownListFor(m => m.Status, ((ShippingStatus)Model.Status).ToSelectList())
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.StatusUpdateValidForCommission):
                </td>
                <td class="adminData">
                    @Html.CheckBoxFor(m => m.StatusUpdateValidForCommission)
                </td>
            </tr>
        }
        @Html.HiddenFor(m => m.UpdatePackage)
        @Html.HiddenFor(m => m.UpdatePickUpLocation)
        @Html.HiddenFor(m => m.UpdateDeliveryLocation)
        <tr>
            @if (Model.UpdatePickUpLocation)
            {
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.PickupRank):
                </td>
                <td class="adminData">
                    @Html.TextBoxFor(m => m.PickupRank)
                </td>

            }
        </tr>
        <tr>
            @if (Model.UpdateDeliveryLocation)
            {
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.DeliveryRank):
                </td>
                <td class="adminData">
                    @Html.TextBoxFor(m => m.DeliveryRank)
                </td>

            }
        </tr>
        <tr>
            @if (Model.ReadyTime.HasValue)
            {
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.ReadyTimeStr):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(m => m.ReadyTimeStr) - @Html.DisplayFor(m => m.PickupEndTimeStr)
                </td>
            }
        </tr>

        <tr>
            @if (Model.InsuranceValue.HasValue)
            {
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.InsuranceValue):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(m => m.InsuranceValue)
                </td>
            }
        </tr>


        @if (Model.PlanId.HasValue)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.PlanId):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(m => m.PlanId)
                    @Html.HiddenFor(m => m.PlanId)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(m => m.PlanStatusString):
                </td>
                <td class="adminData">
                    @Html.DisplayFor(m => m.PlanStatusString)
                </td>
            </tr>
        }

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.SelectedPlanId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(m => m.SelectedPlanId, Model.AvailablePlanIds)
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.TrackingNumber):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.TrackingNumber)
            </td>
        </tr>
        
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.ScheduledTime):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.ScheduledTime)
            </td>
        </tr>
        <tr class="adminSeparator">
            <td colspan="2">
                <hr />
            </td>
        </tr>
        <tr>
            <td class="adminTitle">@T("Admin.Logistics.packages.DimensionAndWeight"):</td>
            <td class="adminData">
                @if (Model.Length.HasValue && Model.Width.HasValue && Model.Height.HasValue)
                {
                    <span>
                        Package Dimension (@Html.DisplayFor(m => m.LinearMeasureUnit)): &nbsp; @Html.DisplayFor(m => m.Length) &nbsp; x &nbsp; @Html.DisplayFor(m => m.Width) &nbsp; x &nbsp; @Html.DisplayFor(m => m.Height) &nbsp;&nbsp; (Length x Width x Height)<br />
                        Package Weight (@Html.DisplayFor(m => m.WeightMeasureUnit)): &nbsp; @Html.DisplayFor(m => m.TotalWeight)
                    </span>
                }
                else if (Model.SpotshubPackageSize == 2)
                {
                    @T("Logistics.Order.Package.CompactSize")
                }
                else if (Model.SpotshubPackageSize == 3)
                {
                    @T("Logistics.Order.Package.FullSize")
                }
                else
                {
                    <span>
                        @T("Logistics.Order.Package.Size.Letter")
                    </span>
                }
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.Attribute):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.Attribute)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.CreatedOn):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.CreatedOn)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.UpdatedOn):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.UpdatedOn)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.Reference):
            </td>
            <td class="adminData">
                @Html.DisplayFor(m => m.Reference)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(m => m.DeliveryAttemptCount):
            </td>
            <td class="adminData">
                @Html.EditorFor(m => m.DeliveryAttemptCount)
            </td>
        </tr>

        <tr class="adminSeparator">
            <td colspan="2">
                <hr />
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @T("Admin.Logistics.packages.OrderBy"):
            </td>
            <td class="adminData">
                <table style="border: solid 1px black; padding: 5px;">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(m => m.CustomerId)
                        </td>
                        <td class="adminData">
                            <a href="@Url.Action("Edit", "Customer", new { id = Model.CustomerId })">@Model.CustomerId</a>
                        </td>
                    </tr>

                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(m => m.CustomerAddress.Email):
                        </td>
                        <td class="adminData">
                            <a href="@Url.Action("Edit", "Customer", new { id = Model.CustomerId })">@Model.CustomerAddress.Email</a>
                        </td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Company").Text:</td>
                        <td class="adminData">@Model.CustomerAddress.Company</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.FirstName").Text:</td>
                        <td class="adminData">@Model.CustomerAddress.FirstName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.LastName").Text:</td>
                        <td class="adminData">@Model.CustomerAddress.LastName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.CellPhoneNumber").Text:</td>
                        <td class="adminData">@Model.CustomerAddress.CellPhoneNumber</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.PhoneNumber").Text:</td>
                        <td class="adminData">@Model.CustomerAddress.PhoneNumber</td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr class="adminSeparator">
            <td colspan="2">
                <hr />
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @T("Admin.Logistics.PickupAddress").Text:
            </td>
            <td class="adminData">
                <table style="border: solid 1px black; padding: 5px;">

                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Company").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.Company</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.FirstName").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.FirstName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.LastName").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.LastName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.CellPhoneNumber").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.CellPhoneNumber</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.PhoneNumber").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.PhoneNumber</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Address1").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.Address1</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Address2").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.Address2</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.City").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.City</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.ZipPostalCode").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.ZipPostalCode</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.StateProvince").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.StateProvinceName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Country").Text:</td>
                        <td class="adminData">@Model.PickUpLocation.Address.CountryName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">
                            <a href="@Url.Action("AddressEdit", "Logistics", new { locationId = Model.PickUpLocation.Id })" class="t-button">@T("Admin.Logistics.Address.EditAddress")</a>
                        </td>
                        <td class="adminData">
                            <img alt="google maps" src="@Url.Content("~/subsites/Administration/Content/images/google-maps.gif")" />
                            <a href="@Html.Raw(Model.PickupAddressGoogleMapsUrl)" target="_blank">@T("Admin.Orders.Fields.ShippingAddress.ViewOnGoogleMaps")</a>
                        </td>
                    </tr>
                </table>
            </td>
        </tr>

        <tr class="adminSeparator">
            <td colspan="2">
                <hr />
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @T("Admin.Logistics.DeliveryAddress").Text:
            </td>
            <td class="adminData">
                <table style="border: solid 1px black; padding: 5px;">

                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Company").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.Company</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.FirstName").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.FirstName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.LastName").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.LastName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.CellPhoneNumber").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.CellPhoneNumber</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.PhoneNumber").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.PhoneNumber</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Address1").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.Address1</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Address2").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.Address2</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.City").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.City</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.ZipPostalCode").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.ZipPostalCode</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.StateProvince").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.StateProvinceName</td>
                    </tr>
                    <tr>
                        <td class="adminTitle">@T("Admin.Address.Fields.Country").Text:</td>
                        <td class="adminData">@Model.DeliveryLocation.Address.CountryName</td>
                    </tr>

                    <tr>
                        <td class="adminTitle">
                            <a href="@Url.Action("AddressEdit", "Logistics", new { locationId = Model.DeliveryLocation.Id })" class="t-button">@T("Admin.Logistics.Address.EditAddress")</a>
                        </td>
                        <td class="adminData">
                            <img alt="google maps" src="@Url.Content("~/subsites/Administration/Content/images/google-maps.gif")" />
                            <a href="@Html.Raw(Model.DeliveryAddressGoogleMapsUrl)" target="_blank">@T("Admin.Orders.Fields.ShippingAddress.ViewOnGoogleMaps")</a>

                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
}

@helper TabPlanNote()
{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;


    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<PlanNoteModel>()
                    .Name("plannotes-grid")
                    .DataKeys(keys =>
                    {
                        keys.Add(x => x.Id).RouteKey("planNoteId");
                        keys.Add(x => x.PlanId).RouteKey("planId");
                    })
                    .DataBinding(binding =>
                    {
                        binding.Ajax().Select("PlanNotesSelect", "Logistics", new { planId = Model.SelectedPlanId })
                            .Update("PlanNotesUpdate", "Logistics");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.CreatedOn)
                            .ReadOnly();
                        columns.Bound(x => x.Action)
                            .ReadOnly();
                        columns.Bound(x => x.PlanId)
                            .ReadOnly();
                        columns.Bound(x => x.PlanStatus)
                            .ReadOnly();
                        columns.Bound(x => x.PackageId)
                            .ReadOnly();
                        columns.Bound(x => x.PackageStatus)
                            .ReadOnly();
                        columns.Bound(x => x.LocationId)
                            .ReadOnly();
                        columns.Bound(x => x.Note).ReadOnly().Encoded(false).Width(500);
                        columns.Bound(x => x.ValidForCommission);
                        columns.Command(commands =>
                        {
                            commands.Edit().Text(T("Admin.Common.Edit").Text);
                        }).Width(100);
                    })
                    .Editable(x =>
                    {
                        x.Mode(GridEditMode.InLine);
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true)
                    .ClientEvents(x => x.OnError("Grid_onError")))
            </td>
        </tr>
    </table>
    <div class="clear">
    </div>
    <script type="text/javascript">
        function Grid_onError(e) {
            alert(e.XMLHttpRequest.responseText);
            e.preventDefault();
        }
    </script>
}

@helper TabPackageNote()
{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    <table class="adminContent">
        <tr>
            <td>
                @(Html.Telerik().Grid<PackageNoteModel>()
                    .Name("packagenotes-grid")
                    .DataKeys(keys =>
                    {
                        keys.Add(x => x.Id).RouteKey("packageNoteId");
                        keys.Add(x => x.PackageId).RouteKey("packageId");
                    })
                    .DataBinding(binding =>
                    {
                        binding.Ajax().Select("PackageNotesSelect", "Logistics", new { packageId = Model.Id })
                            .Update("PackageNotesUpdate", "Logistics");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.CreatedOn)
                            .ReadOnly()
                            .Width(100);
                        columns.Bound(x => x.CustomerEmail)
                            .ReadOnly()
                            .Template(x => Html.ActionLink(x.CustomerEmail, "Edit", "Customer", new { id = x.CustomerId }, new { }))
                            .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Customer/Edit/") + "<#= CustomerId #>\"><#= CustomerEmail #></a>")
                            .Width(200);
                        columns.Bound(x => x.Note).ReadOnly().Encoded(false).Width(300);
                        columns.Bound(x => x.Type)
                            .ReadOnly()
                            .Template(x => x.TypeString)
                            .ClientTemplate("<#= TypeString #>");
                        columns.Bound(x => x.ShippingStatus)
                            .ReadOnly();
                        columns.Bound(x => x.Address)
                            .ReadOnly()
                            .Template(x => x.AddressString)
                            .ClientTemplate("<#= AddressString #>");
                        columns.Bound(x => x.DisplayToCustomer)
                            .EditorTemplateName("Checkbox")
                            .Centered()
                            .Width(100);
                        columns.Command(commands =>
                        {
                            commands.Edit().Text(T("Admin.Common.Edit").Text);
                        }).Width(100);
                    })
                    .Editable(x =>
                    {
                        x.Mode(GridEditMode.InLine);
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true)
                    .ClientEvents(x => x.OnError("Grid_onError")))
            </td>
        </tr>
    </table>
    <div class="clear">
    </div>
    <p>
        <strong>
            @T("Admin.Logistics.Common.Fields.AddPkgNote").Text:
        </strong>
    </p>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.LabelFor(m => m.PackageNoteInput):
            </td>
            <td class="adminData">
                @Html.TextAreaFor(m => m.PackageNoteInput, 5, 50, new { placeholder = "Enter here ...", style = "vertical-align: middle" })
            </td>
        </tr>

        <tr>
            <td class="adminTitle">
                @T("Admin.Logistics.Common.Fields.AddPackageNoteDisplayToCustomer").Text:
            </td>
            <td class="adminData">
                @Html.EditorFor(m => m.AddPackageNoteDisplayToCustomer)
                @Html.ValidationMessageFor(m => m.AddPackageNoteDisplayToCustomer)
            </td>
        </tr>
        <tr>
            <td colspan="2" align="left">
                <input type="button" id="addPackageNote" class="t-button" value="@T("Admin.Logistics.PackageNotes.AddButton")" />
            </td>
        </tr>
    </table>

    <script type="text/javascript">
        $(document).ready(function () {
            $('#addPackageNote').click(function () {
                var packageNoteMessage = $("#@Html.FieldIdFor(model => model.PackageNoteInput)").val();
                var packageNoteDisplayToCustomer = $("#@Html.FieldIdFor(model => model.AddPackageNoteDisplayToCustomer)").is(':checked');
                $('#addPackageNote').attr('disabled', true);
                $.ajax({
                    cache:false,
                    type: "POST",
                    url: "@(Url.Action("PackageNoteAdd", "Logistics"))",
                    data: { "DisplayToCustomer": packageNoteDisplayToCustomer, "message": packageNoteMessage, "packageId": @Model.Id },
                    success: function (data) {
                        if (data.Result) {
                            var packagenotesGrid = $("#packagenotes-grid");
                            packagenotesGrid.data('tGrid').ajaxRequest();
                            $('#addPackageNote').attr('disabled', false);
                        }
                        else {
                            alert('Failed to add package note.');
                        }
                    },
                    error:function (xhr, ajaxOptions, thrownError){
                        alert('Failed to add package note.');
                        $('#addPackageNote').attr('disabled', false);
                    }
                });
            });
        });

        function Grid_onError(e) {
            alert(e.XMLHttpRequest.responseText);
            e.preventDefault();
        }
    </script>
}
